package com.grad.webstudy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grad.webstudy.constants.RoleConstants;
import com.grad.webstudy.context.UserContext;
import com.grad.webstudy.dao.CoursecommentDao;
import com.grad.webstudy.enums.ExceptionEnums;
import com.grad.webstudy.exceptions.CustomException;
import com.grad.webstudy.model.CourseComment;
import com.grad.webstudy.model.User;
import com.grad.webstudy.service.CourseCommentService;
import com.grad.webstudy.service.RoleService;
import com.grad.webstudy.service.RoleUserService;
import com.grad.webstudy.service.UserService;
import com.grad.webstudy.vo.CourseCommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/15 15:26.
 */

@Service
@Transactional
public class CourseCommentServiceImpl implements CourseCommentService {


    @Autowired
    private CoursecommentDao coursecommentDao;


    @Autowired
    private RoleUserService roleUserService;


    @Autowired
    private RoleService roleService;


    @Autowired
    private UserService userService;


    @Override
    public boolean addComment(String content, Integer detailId) {

        User loginUser = UserContext.getLoginUser();

        CourseComment courseComment = new CourseComment.Builder()
                .setCourseDetailId(detailId)
                .setContent(content)
                .setUserId(loginUser.getId())
                .build();

        return coursecommentDao.insert(courseComment) > 0;
    }

    @Override
    public boolean deleteByIds(String ids, boolean isAdminDo) {

        List<String> deleteIds = Arrays.asList(ids.split(","));

        if (deleteIds.size() <= 0) {
            throw new CustomException(ExceptionEnums.CUSTOM_ERROR.getCode(),"传入的参数ids[" + ids + "]有误");
        }
        User loginUser = UserContext.getLoginUser();

        List<Integer> roleId = roleUserService.queryRoleIdsByUserId(loginUser.getId());

        if (roleId.contains(RoleConstants.ADMIN) && isAdminDo) {
            return coursecommentDao.deleteByIds(deleteIds, null) == deleteIds.size();
        }
        return coursecommentDao.deleteByIds(deleteIds, loginUser.getId()) == deleteIds.size();

    }

    @Override
    public PageInfo queryByDetailId(Integer detailId, int currPage, int pageSize) {


        PageHelper.startPage(currPage, pageSize);

        PageInfo<CourseCommentVo> pageInfo = new PageInfo<>(
                transCommentToVo(null, coursecommentDao.selectByCourseDetailId(detailId))
        );
        return pageInfo;
    }

    @Override
    public PageInfo queryByUserId(Integer userId, int currPage, int pageSize) {
        PageHelper.startPage(currPage, pageSize);

        PageInfo<CourseCommentVo> pageInfo = new PageInfo<>(
                transCommentToVo(userId, coursecommentDao.selectByUserId(userId))
        );

        return pageInfo;
    }

    @Override
    public PageInfo queryAll(int currPage, int pageSize) {

        PageHelper.startPage(currPage, pageSize);

        PageInfo<CourseCommentVo> pageInfo = new PageInfo<>(
                transCommentToVo(null, coursecommentDao.queryAll())
        );

        return pageInfo;
    }


    private List<CourseCommentVo> transCommentToVo(Integer userId, List<CourseComment> courseComments) {

        List<CourseCommentVo> result = new ArrayList<>(courseComments.size());


        User user = new User();
        if (userId != null) {
            user = userService.selectUserForSystem(userId);
        }
        for (int i = 0; i < courseComments.size(); i++) {
            CourseComment courseComment = courseComments.get(i);
//            CourseComment nextComment=
            // TODO: 2020/2/15 优化循环查询，下一个userId相同时不用查询
            if (userId == null) {
                user = userService.selectUserForSystem(courseComment.getUserId());
            }


            result.add(new CourseCommentVo.Builder()
                    .setTime(courseComment.getTime())
                    .setHeadImage(user.getImagePath())
                    .setUserName(user.getUsername())
                    .setContent(courseComment.getContent())
                    .setCourseDetailId(courseComment.getCourseDetailId())
                    .setId(courseComment.getId())
                    .setUserId(user.getId()).build());
        }


        return result;
    }
}
